home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.04 Apr 94 / Accurate Timing / GatherTimes.c next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  3.0 KB  |  173 lines  |  [TEXT/KAHL]

  1. /* GatherTimes ---------------------------------------------------
  2.  *
  3.  * Demo precision timing experiments.
  4.  * We collect raw time measurements, then call the
  5.  * TStats package to display results.
  6.  *
  7.  * For your functions, you must:
  8.  * 1) #include their headers.
  9.  * 2) #define Arglist, Fun1, Fun2 macros with the specs
  10.  *        for your functions (as shown for Foo and FooBar).
  11.  * 3) declare and init data needed by your functions in main.
  12.  * 4) init anything else your functions need before the
  13.  *        Loop macro (as we set arg1 = arg2 = 1).
  14.  *
  15.  * You need not modify anything else except the testing
  16.  * parameters {Which, Precision, Accuracy}.
  17.  *
  18.  * Copyright (c) 1993 Bill Karsh.
  19.  * All rights reserved.
  20.  *
  21.  */
  22.  
  23.  
  24. #pragma options( !check_ptrs )
  25.  
  26.  
  27. #include    "TestFuncs.h"
  28. #include    "TStats.h"
  29. #include    <Timer.h>
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. // competitors to test
  37.  
  38. #define        First            1
  39. #define        Second            2
  40. #define        Both            3
  41.  
  42. #define        Which            Both
  43.  
  44. #define        ArgList            ( &arg1, &arg2 )
  45. #define        Fun1            Foo
  46. #define        Fun2            FooBar
  47.  
  48. //----------------------------------------------------------------
  49.  
  50. // timing parameters
  51.  
  52. #define        MaxNeg            0x80000000
  53.  
  54. #define        Precision        100
  55. #define        Accuracy        1000    // must be > 0
  56.  
  57. //----------------------------------------------------------------
  58.  
  59. // timing macros and glue
  60.  
  61. static void Overhead( ... )
  62. {
  63.     // always empty
  64. }
  65.  
  66. // call once to fill caches
  67. // call repeatedly to gather timing data
  68.  
  69. #define    Loop( F, T )                                    \
  70.             F ArgList;                                    \
  71.                                                         \
  72.             tmt.tmWakeUp = tmt.tmReserved = 0L;            \
  73.             InsXTime( &tmt );                            \
  74.             PrimeTime( &tmt, MaxNeg );                    \
  75.             for( prc = 0; prc < Precision; prc++ ) {    \
  76.                 F ArgList;                                \
  77.             }                                            \
  78.             RmvTime( &tmt );                            \
  79.             T = tmt.tmCount - MaxNeg
  80.             
  81. #define    WAIT                                            \
  82.         Delay( 20, &dum ); while( !Button() )
  83.             
  84. //----------------------------------------------------------------
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. void main( void )
  92. {
  93. //--- specific args for your functions
  94.  
  95.     long    arg1, arg2;
  96.     
  97. //--- timer args
  98.  
  99.     long    prc, acc;
  100.     long    time1 = 0, time2 = 0, timeOv;
  101.     long    dum;
  102.     TMTask    tmt;
  103.     Boolean    done;
  104.     
  105. // initializations
  106.  
  107.     InitGraf( &thePort );
  108.     InitFonts();
  109.     InitWindows();
  110.     InitMenus();
  111.     TEInit();
  112.     InitDialogs( nil );
  113.     InitCursor();
  114.         
  115.     tmt.tmAddr = nil;
  116.     
  117.     TSInit( nil, Accuracy, Accuracy );
  118.  
  119. //----------------------------------------------------------------
  120.  
  121.     for( acc = 0; acc < Accuracy; acc++ ) {
  122.     
  123. #if Which & First
  124.         // init data for your Fun1
  125.         arg1 = 1;
  126.         arg2 = 1;
  127.             
  128.         Loop( Fun1, time1 );
  129. #endif
  130.  
  131.  
  132. #if Which & Second
  133.         // init data for your Fun2
  134.         arg1 = 1;
  135.         arg2 = 1;
  136.             
  137.         Loop( Fun2, time2 );
  138. #endif
  139.  
  140.         Loop( Overhead, timeOv );
  141.         if( time1 > timeOv ) time1 -= timeOv;
  142.         if( time2 > timeOv ) time2 -= timeOv;
  143.     
  144.         TSAccumulate( time1, time2 );
  145.     }
  146.     
  147. //----------------------------------------------------------------
  148.  
  149.     TSRawPlots();
  150.     TSStats( kRaw );
  151.     WAIT;
  152.     
  153.     TSRawHistos();
  154.     WAIT;
  155.     
  156.     done = TSFilterMode( kRaw );
  157.     TSStats( kWork );
  158.     WAIT;
  159.     
  160.     if( !done ) {
  161.         do {
  162.             done = TSFilterMode( kWork );
  163.             TSStats( kWork );
  164.             WAIT;
  165.         } while( !done );
  166.     }
  167.     
  168.     TSDispose();
  169. }
  170.  
  171.  
  172.  
  173.